/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdMath.h"
#include "hhdMatrix4.h"

#if HHD_PLATFORM_WINDOWS
#include  <intrin.h>
#endif //HHD_PLATFORM_WINDOWS

namespace hhd
{
	const float Math::Pi  = 3.14159265f;
	const float Math::Epsilon = 0.0001f;
		
	Random** Math::_randoms = NULL;

	float Math::clamp(float min, float max, float value)
	{
		if(value > max)
			value = max;
		else if(value < min)
			value = min;
		return value;
	}

	int Math::clamp(int min, int max, int value)
	{
		if(value > max)
			return max;
		if(value < min)
			return min;
		return value;
	}

	float Math::degreesToRadians(float in_theta)
	{
		return in_theta  * (Math::Pi / 180.0f);
	}
	/// ax^2 + bx + c
	int Math::solveQuadratic(float in_a, float in_b, float in_c, float& out_x1, float& out_x2)
	{
		// linear equation
		if(in_a == 0.0f)
		{
			out_x1 = (-in_c) / in_b;
			return 1;
		}

		float b2_4ac = in_b * in_b - 4.0f * in_a * in_c;
		if(b2_4ac < 0)
		{
			return 0;
		}

		float sqrt_b2_4ac = sqrt(b2_4ac);
		out_x1 = (-in_b - sqrt_b2_4ac) / (2.0f * in_a);
		out_x2 = (-in_b + sqrt_b2_4ac) / (2.0f * in_a);
		
		return 2;
	}

	float Math::lerp(float in_start, float in_end, float t)
	{
		if(t < 0.0f)
			t = 0.0f;
		else if( t > 1.0f)
			t = 1.0f;

		return (1-t)*in_start + t*in_end;
	}

	bool Math::equal(float a, float b, float error, bool errorAbsolute)
	{
		if(a == b) return true;

		if(errorAbsolute)	// check using absolute error
		{
			return fabs(a - b) < error;
		}
		else	// check using relative error
		{
			float relativeError;
			if (fabs(b) > fabs(a))
				relativeError = fabs((a - b) / b);
			else
				relativeError = fabs((a - b) / a);
			return relativeError <= error;
		}
	}

	Random& Math::random()
	{
		//baw	come back to this and do something useful later.
		if(_randoms == NULL)
		{
			_randoms = new Random*[1];
			_randoms[0] = new Random();
		}

		return *_randoms[0];
	}

	void Math::initialize()
	{
#if HHD_PLATFORM_WINDOWS

		// figure out which matrix mul to use
		// http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
		int CPUInfo[4] = {-1};
		__cpuid(CPUInfo, 1);

		// SSE41
		if(CPUInfo[2] & 0x80000)
		{
			Matrix4::matrix_multiply = Matrix4::sse41_multiply;
			//alert("Using SSE 4.1 Matrix Multiply");
		}
		// SSE3
		else if(CPUInfo[2] & 0x1)
		{
			Matrix4::matrix_multiply = Matrix4::sse3_multiply;
			//alert("Using SSE 3 Matrix Multiply");
		}
		else
		{
			Matrix4::matrix_multiply = Matrix4::slow_multiply;
			//alert("Using Slow Matrix Multiply");
		}
#endif //HHD_PLATFORM_WINDOWS			
	}

	void Math::finalize()
	{
		if(_randoms != NULL)
		{
			delete _randoms[0];
			delete[] _randoms;
			_randoms = NULL;
		}
	}
}
